package fr.imag.wifibot.client;

import java.io.IOException;
import java.net.Socket;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

public class Client {
	public static String IP = "192.168.1.151";

	public static int HOST = 6666;
	
	public static Logger logger;

	static {
	    try {
	    	int limit = 1000000;
	    	int numLogFiles = 100;
	    	boolean append = true;
		      FileHandler fh = new FileHandler("wifibot.log", limit, numLogFiles, append);
		      //fh.setFormatter(new XMLFormatter());
		      fh.setFormatter(new SimpleFormatter());
		      logger = Logger.getLogger("TestLog");
		      logger.addHandler(fh);
	    }
	    catch (Exception e) {
	    	e.printStackTrace();
	    }
	}
	
	private Shell shell;
	private Display display;
//	private long startTime = System.currentTimeMillis();
	
	private static int NB_SENSOR = 6;
	
	private static Color MQ4Color;
	private static Color MQ6Color;
	private static Color MQ7Color;
	private static Color PotentiometreColor;
	private static Color GeigerColor;
	private static Color GPSColor;
	
	private List lMQ4, lMQ6, lMQ7, lPOT, lGEI, lGPS;
	private Label labMQ4,labMQ6, labMQ7, labPOT, labGEI, labGPS;
	
	private Socket socket;

	Client(){
		display = new Display();
		
		MQ4Color = new Color(display, 0, 255, 0);
		MQ6Color = new Color(display, 255, 0, 0);
		MQ7Color = new Color(display, 0, 0, 255);
		PotentiometreColor = new Color(display, 0, 0, 0);
		GeigerColor = new Color(display, 255, 0, 255);
		GPSColor = new Color(display, 128, 128, 128);
		
		shell = new Shell(display);
		shell.setText("Client");
		
		GridLayout g = new GridLayout();
		g.makeColumnsEqualWidth = true;
		g.numColumns = NB_SENSOR;
		shell.setLayout(g);
		
		//Set the background color
		shell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
	
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		//Create the different label 
		//One for each sensor
		
		GridData gridDataLabel = new GridData(GridData.FILL_HORIZONTAL);
		
		labMQ4 = new Label(shell, SWT.NONE);
		labMQ4.setForeground(MQ4Color);
		labMQ4.setText(" MQ4");
		labMQ4.setLayoutData(gridDataLabel);
		
		labMQ6 = new Label(shell, SWT.NONE);
		labMQ6.setForeground(MQ6Color);
		labMQ6.setText(" MQ6");
		labMQ6.setLayoutData(gridDataLabel);
		
		labMQ7 = new Label(shell, SWT.NONE);
		labMQ7.setForeground(MQ7Color);
		labMQ7.setText(" MQ7");
		labMQ7.setLayoutData(gridDataLabel);
		
		labPOT = new Label(shell, SWT.NONE);
		labPOT.setForeground(PotentiometreColor);
		labPOT.setText(" Potent");
		labPOT.setLayoutData(gridDataLabel);
		
		labGEI = new Label(shell, SWT.NONE);
		labGEI.setForeground(GeigerColor);
		labGEI.setText(" Geiger");
		labGEI.setLayoutData(gridDataLabel);
		
		labGPS = new Label(shell, SWT.NONE);
		labGPS.setForeground(GPSColor);
		labGPS.setText(" GPS");
		labGPS.setLayoutData(gridDataLabel);
		
		//Create the different list
		//One for each sensor
		
		GridData gridDataList = new GridData(GridData.FILL_BOTH);
		gridDataList.grabExcessVerticalSpace = true;
		gridDataList.grabExcessHorizontalSpace = true;
		
		lMQ4 = new List(shell, SWT.BORDER);
		lMQ4.setForeground(MQ4Color);
		lMQ4.setLayoutData(gridDataList);
		
		lMQ6 = new List(shell, SWT.BORDER);
		lMQ6.setForeground(MQ6Color);
		lMQ6.setLayoutData(gridDataList);
		
		lMQ7 = new List(shell, SWT.BORDER);
		lMQ7.setForeground(MQ7Color);
		lMQ7.setLayoutData(gridDataList);
		
		lPOT = new List(shell, SWT.BORDER);
		lPOT.setForeground(PotentiometreColor);
		lPOT.setLayoutData(gridDataList);
		
		lGEI = new List(shell, SWT.BORDER);
		lGEI.setForeground(GeigerColor);
		lGEI.setLayoutData(gridDataList);
		
		lGPS = new List(shell, SWT.BORDER);
		lGPS.setForeground(GPSColor);
		lGPS.setLayoutData(gridDataList);
		
		shell.pack();
		shell.setSize(shell.getBounds().width, 500);
		shell.open();
		
		ListenThread l = new ListenThread(this);
		l.start();
		
		//Create and check the event loop
		while (!shell.isDisposed()) {		
			if (!display.readAndDispatch())
				display.sleep();
		}	
		try {
			socket.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		try {
			l.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		display.dispose();
	}
	
	public void decodeAndPrint(String msg){
		logger.info(msg);
		String[] s = msg.split(",");		
		if(s[0].equals("$MQ4")){
			lMQ4.add(s[1], 0);
		}
		else if(s[0].equals("$MQ6")){
			lMQ6.add(s[1], 0);
		}
		else if(s[0].equals("$MQ7")){
			lMQ7.add(s[1], 0);
		}
		else if(s[0].equals("$POT")){
			lPOT.add(s[1], 0);
		}
		else if(s[0].equals("$GEI")){
			lGEI.add(s[1], 0);
		}
		else if(s[0].equals("$GPGGA")){
			lGPS.add(s[1] + "," + s[6] + "," + s[7] + "," + s[10] + "," + s[11] + "," + s[12] + "," + s[14], 0);
		}
	}
	
	public void setSocket(Socket s){
		socket = s;
	}
	
	public static void main(String[] args) {
		if(args.length == 1){
			IP = args[0];
		}
		else if(args.length == 2){
			IP = args[0];
			HOST = Integer.parseInt(args[1]);
		}
		Client c = new Client();
	}
		
}
